package com.kaixinff.kaixin001.servlet;

import java.io.IOException;
import java.lang.Thread.State;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaixinff.kaixin001.util.Filter;
import com.kaixinff.kaixin001.util.KXUtil;
import com.kaixinff.kaixin001.util.RegStep;
import com.kaixinff.kaixin001.xw.MainThread;
import com.kaixinff.name.User;

@SuppressWarnings("serial")
public class XWServlet extends HttpServlet {
	private static final int TASK_TYPES = 12;
	private static final byte[] lock = new byte[0];
	private static MainThread mainThread = null;
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html; charset=" + KXUtil.CHARSET);
		List<User> users = KXUtil.getUsers(new Filter(){
			@Override
			public boolean access(User user) {
				return user.hasStep(RegStep.ADD_XW);
			}}, resp);
		if (users == null) {
			return;
		}
		
		resp.getWriter().println("<form name='form' method='post' action='/kx/xw'>");
		resp.getWriter().println("<input type=checkbox name='actions' value='0'> Do job<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='1'> Do job 2<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='2'> Fight<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='3'> Help<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='4'> Exchange<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='5'> Secret bag<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='6'> Dianchi<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='7'> Mangu<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='8'> Property<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='9'> Gift free<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='10'> Consume<br>");
		resp.getWriter().println("<input type=checkbox name='actions' value='11'> Gaming<br>");
		resp.getWriter().println("<input type=submit name='submit' value='start'>");
		resp.getWriter().println("</form>");
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		String [] actions = req.getParameterValues("actions");
		if (actions != null && actions.length > 0) {
			List<User> users = KXUtil.getUsers(new Filter(){
				@Override
				public boolean access(User user) {
					return user.hasStep(RegStep.ADD_XW);
				}}, resp);
			boolean[] flags = new boolean[TASK_TYPES];
			for (int i = 0, n = actions.length; i < n; i++) {
				flags[Integer.parseInt(actions[i])] = true;
			}
			synchronized (lock) {
				if (mainThread == null || State.TERMINATED.equals(mainThread.getState())) {
					mainThread = new MainThread(users, flags);
					mainThread.start();
					resp.getWriter().println("Task started!");
				} else {
					resp.getWriter().println("Task was already started!");
				}
			}
			
		} else {
			resp.getWriter().println("No task to run!");
		}
	}
}
